use iced::{Element, Length, Padding};
use iced::widget::{container, text, Container};
use crate::theme::CdbTheme;
use super::{spacing, radius, typography};

/// Badge variants
#[derive(Debug, Clone, Copy)]
pub enum BadgeVariant {
    Default,
    Secondary,
    Destructive,
    Outline,
    Success,
    Warning,
}

/// Badge sizes
#[derive(Debug, Clone, Copy)]
pub enum BadgeSize {
    Small,
    Medium,
    Large,
}

/// Create a modern badge component
pub fn badge<'a, Message>(
    content: &'a str,
    variant: BadgeVariant,
    size: BadgeSize,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    let padding = match size {
        BadgeSize::Small => Padding::from([spacing::XS, spacing::SM]),
        BadgeSize::Medium => Padding::from([spacing::SM, spacing::MD]),
        BadgeSize::Large => Padding::from([spacing::MD, spacing::LG]),
    };

    let text_size = match size {
        BadgeSize::Small => typography::XS,
        BadgeSize::Medium => typography::SM,
        BadgeSize::Large => typography::BASE,
    };

    let text_element = text(content)
        .size(text_size);

    container(text_element)
        .style(move |_theme| {
            let colors = &theme.colors;
            
            match variant {
                BadgeVariant::Default => container::Style {
                    background: Some(colors.primary.into()),
                    text_color: Some(colors.primary_foreground),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
                BadgeVariant::Secondary => container::Style {
                    background: Some(colors.secondary.into()),
                    text_color: Some(colors.secondary_foreground),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
                BadgeVariant::Destructive => container::Style {
                    background: Some(colors.destructive.into()),
                    text_color: Some(colors.destructive_foreground),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
                BadgeVariant::Outline => container::Style {
                    background: Some(iced::Color::TRANSPARENT.into()),
                    text_color: Some(colors.foreground),
                    border: iced::Border {
                        color: colors.border,
                        width: 1.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
                BadgeVariant::Success => container::Style {
                    background: Some(colors.chart_2.into()),
                    text_color: Some(colors.primary_foreground),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
                BadgeVariant::Warning => container::Style {
                    background: Some(colors.chart_3.into()),
                    text_color: Some(colors.primary_foreground),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::SM.into(),
                    },
                    shadow: iced::Shadow::default(),
                },
            }
        })
        .padding(padding)
}

/// Create a status badge
pub fn status_badge<'a, Message>(
    status: &'a str,
    is_positive: bool,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    let variant = if is_positive {
        BadgeVariant::Success
    } else {
        BadgeVariant::Destructive
    };
    
    badge(status, variant, BadgeSize::Small, theme)
}

/// Create a connection status badge
pub fn connection_badge<'a, Message>(
    is_connected: bool,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    let (text, variant) = if is_connected {
        ("Connected", BadgeVariant::Success)
    } else {
        ("Disconnected", BadgeVariant::Destructive)
    };
    
    badge(text, variant, BadgeSize::Small, theme)
}

/// Create a database type badge
pub fn database_type_badge<'a, Message>(
    db_type: &'a str,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    badge(db_type, BadgeVariant::Secondary, BadgeSize::Small, theme)
}
